<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <h2>集市交易后台管理系统</h2>
        <p>欢迎回来，请登录您的账户</p>
      </div>
      
      <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            prefix-icon="el-icon-user"
            placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            prefix-icon="el-icon-lock"
            type="password"
            placeholder="请输入密码">
          </el-input>
        </el-form-item>
        
        <el-form-item class="login-buttons">
          <el-button type="primary" @click="submitForm('loginForm')" class="login-btn">登录</el-button>
          <el-button @click="resetForm('loginForm')" class="reset-btn">重置</el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p>© 2025 集市交易平台 版权所有</p>
      </div>
    </div>
    
    <!-- 装饰元素 -->
    <div class="circle circle-1"></div>
    <div class="circle circle-2"></div>
    <div class="circle circle-3"></div>
    <div class="circle circle-4"></div>
  </div>
</template>

<script>
  import moment from "moment";
  export default {
    data() {
      return {
        loginForm: {
          username: '',
          password: '',
        },
        rules: {
          username: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 8,
              message: '长度在 3 到 8 个字符',
              trigger: 'blur'
            }
          ],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'change'
          }]
        }
      }
    },
    created() {
      if(this.$route.params.id == 1){
        this.$store.commit('userInfo','');
        history.pushState(null, null, document.URL);
        window.addEventListener('popstate', function(e) {
            history.pushState(null, null, document.URL);
        });
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios.post('/Badmin/selectAdminByUser',{
              username: this.loginForm.username,
              password: this.loginForm.password
            }).then(res => {
              if(res.data.length != 0){
                //存储用户信息
                this.$store.commit('userInfo',res.data);
                this.$message({
                  type: 'success',
                  message: '登录成功！'
                });

                //修改管理员身份信息
                this.$axios.post('/Badmin/updateAdminById',{
                  id:this.$store.state.userInfo.id,
                  logintime: moment().format('YYYY-MM-DD HH:mm:ss'),
                  status:1
                });

                this.$router.push('/index');
              }else{
                this.$message({
                  type: 'warning',
                  message: '用户名或密码出错，请重新输入！'
                });
              }
            })
          } else {
            this.$message({
              type: 'warning',
              message: '请完善登录信息！'
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    }
  }
</script>

<style>
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.login-container {
  width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 2;
  backdrop-filter: blur(10px);
  animation: fadeIn 0.8s ease-out;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form .el-form-item {
  margin-bottom: 20px;
}

.login-form .el-input__inner {
  height: 50px;
  border-radius: 8px;
  font-size: 14px;
  border: 1px solid #dcdfe6;
  box-shadow: none;
  transition: all 0.3s;
}

.login-form .el-input__inner:focus {
  border-color: #764ba2;
  box-shadow: 0 0 0 2px rgba(118, 75, 162, 0.2);
}

.login-form .el-input__prefix {
  left: 12px;
}

.login-form .el-form-item__error {
  padding-top: 4px;
  color: #f56c6c;
}

.login-buttons {
  margin-top: 30px;
  display: flex;
  justify-content: space-between;
  gap: 15px;
}

.login-btn, .reset-btn {
  flex: 1;
  height: 46px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.login-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(118, 75, 162, 0.3);
}

.reset-btn {
  background: transparent;
  color: #666;
  border: 1px solid #dcdfe6;
}

.reset-btn:hover {
  color: #764ba2;
  border-color: #764ba2;
  background: rgba(118, 75, 162, 0.05);
}

.login-footer {
  text-align: center;
  margin-top: 30px;
}

.login-footer p {
  color: #999;
  font-size: 12px;
}

/* 装饰性圆圈 */
.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
  animation: float 8s infinite ease-in-out;
}

.circle-2 {
  width: 200px;
  height: 200px;
  top: 60%;
  right: -50px;
  animation: float 10s infinite ease-in-out;
}

.circle-3 {
  width: 150px;
  height: 150px;
  bottom: -50px;
  left: 30%;
  animation: float 12s infinite ease-in-out;
}

.circle-4 {
  width: 80px;
  height: 80px;
  top: 20%;
  right: 25%;
  animation: float 9s infinite ease-in-out;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(10deg);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .login-container {
    width: 90%;
    max-width: 360px;
    padding: 30px 20px;
  }
  
  .login-buttons {
    flex-direction: column;
    gap: 10px;
  }
  
  .login-btn, .reset-btn {
    width: 100%;
    margin: 0;
  }
  
  .login-header h2 {
    font-size: 20px;
  }
}

/* 小屏幕适配 */
@media screen and (max-width: 480px) {
  .login-container {
    padding: 25px 15px;
  }
  
  .login-form .el-input__inner {
    height: 44px;
  }
  
  .login-btn, .reset-btn {
    height: 40px;
    font-size: 14px;
  }
}
</style>
